<?php

/**
 * File to handle all API requests
 * Accepts GET and POST
 * 
 * Each request will be identified by TAG
 * Response will be JSON data

  /**
 * check for POST request 
 */
if (isset($_REQUEST['tag']) && $_REQUEST['tag'] != '') {
    // get tag
	require_once 'include/CL_Functions.php';
    $tag = $_REQUEST['tag'];
	$func = new CL_Functions();
    // include db handler
    require_once 'include/DB_Functions.php';
    $db = new DB_Functions();

    // response Array
    $response = array("tag" => $tag, "success" => 0, "error" => 0);

    // check for tag type
    if ($tag == 'login') {
        // Request type is check Login
        $email = $_REQUEST['email'];
        $password = $_REQUEST['password'];

        // check for user
        $user = $db->getUserByEmailAndPassword($email, $password);
	    if ($user != false) {
			if($user["activated"] == 0){
			$response["error"] = 1;
			$response["error_msg"] = "Account is awaiting activation, please check the email account associated with it.";
			echo json_encode($response);
		}
		else{
            // user found
            // echo json with success = 1
            $response["success"] = 1;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
		}
        } else {
            // user not found
            // echo json with error = 1
            $response["error"] = 1;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
        }
    } else if ($tag == 'register') {
        // Request type is Register new user
        $name = $_REQUEST['name'];
        $email = $_REQUEST['email'];
        $password = $_REQUEST['password'];

        // check if user is already existed
        if ($db->isUserExisted($email)) {
            // user is already existed - error response
            $response["error"] = 2;
            $response["error_msg"] = "User already existed";
            echo json_encode($response);
        } else {
            // store user
            $user = $db->storeUser($name, $email, $password);
            if ($user) {
                // user stored successfully
                $response["success"] = 1;
                $response["uid"] = $user["unique_id"];
                $response["user"]["name"] = $user["name"];
                $response["user"]["email"] = $user["email"];
                $response["user"]["created_at"] = $user["created_at"];
                $response["user"]["updated_at"] = $user["updated_at"];
                echo json_encode($response);
            } else {
                // user failed to store
                $response["error"] = 1;
                $response["error_msg"] = "Error occured in registration.";
                echo json_encode($response);
            }
        }
    } else if ($tag == 'motd'){
		$motd = $db->getMOTD();
		if ($motd != NULL){
			$response["success"] = 1;
			$response["uid"] = $motd["uid"];
			$response["title"] = $motd["title"];
			$response["message"] = $motd["message"];
			$response["posted"] = $motd["posted"];
			echo json_encode($response);
		}
		else{
			$response["error"] = 1;
			$response["error_msg"] = "No motd to send.";
			echo json_encode($response);
		}
	} else if ($tag == 'device'){
		$uid = $_REQUEST['unique_id'];
		$serial = $_REQUEST['serial'];
		$model = $_REQUEST['model'];
		$version = $_REQUEST['version'];
		$device = $db->storeDevice($uid,$serial,$model,$version);
		if ($device){
			$response["success"] = 1;
			echo json_encode($response);
		}
		else{
			$response["error"] = 1;
			$response["error_msg"] = "Error inserting data.";
			echo json_encode($response);
		}
	} else if ($tag == 'activate'){
		$uid = $_REQUEST['uid'];
		$device = $db->activateAccount($uid);
		var_dump($device);
		if ($device){
			$response["success"] = 1;
			echo json_encode($response);
		} else if ($device == "activated"){
			$response["error"] = 1;
			$response["error_msg"] = "Account already active.";
			echo json_encode($response);
		} else{
			$response["error"] = 1;
			$response["error_msg"] = "Error activating account.";
			echo json_encode($response);
		}
	} else if ($tag == 'resendactivation'){
		$email = $_REQUEST['email'];
		$result = $func->resendActivation($email);
		if ($result){
			$response["success"] = 1;
			echo json_encode($response);
		} else{
			$response["error"] = 1;
			$response["error_msg"] = "Error sending reactivation.";
			echo json_encode($response);
		}
		
	} else if ($tag == 'location'){
		$uid = $_REQUEST['unique_id'];
		$serial = $_REQUEST['serial'];
		$long = $_REQUEST['long'];
		$lat = $_REQUEST['lat'];
		$alt = $_REQUEST['alt'];
		$device = $db->updateLocation($uid,$serial,$long,$lat,$alt);
		if ($device){
			$response["success"] = 1;
			echo json_encode($response);
		}
		else{
			$response["error"] = 1;
			$response["error_msg"] = "No handset available.";
			echo json_encode($response);
		}
	} else {
        echo "Invalid Request";
    }
} else {
    echo "Access Denied";
}
?>
